package com.cdsf.kennedy.user.service.impl;

import com.cdsf.common.conf.AbstractService;
import com.cdsf.kennedy.user.domain.User;
import com.cdsf.kennedy.user.domain.UserRole;
import com.cdsf.kennedy.user.mapper.UserMapper;
import com.cdsf.kennedy.user.mapper.UserRoleMapper;
import com.cdsf.kennedy.user.service.UserRoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: kennedy
 * @Date: Created in 15:55 2018/2/6
 */
@Service
public class UserRoleServiceImpl extends AbstractService implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;


    @Override
    public void assignRoles(Map<String, Object> param) {
        List<String> userIds = (List<String>) param.get("userIds");
        List<String> roleIds = (List<String>) param.get("roleIds");
        assertNotEmpty(userIds, "用户ID不能为空");
        assertNotEmpty(roleIds, "角色ID不能为空");
        List<UserRole> userRoles = new ArrayList<>();
        for (String userId : userIds) {
            // 删除用户以前的角色
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRoleMapper.delete(userRole);
            for (String roleId : roleIds) {
                userRoles.add(new UserRole(userId, roleId, new Date()));
            }
        }
        userRoleMapper.insertList(userRoles);
    }
}
